<template>
  <Container class="common-list bg-light">
    <!-- <div class="container-header group" slot="header">
      <Input class="start" v-model.trim="keyword" placeholder="政策名称" @on-enter="onSearchBtnClick"/>
      <Button type="primary" icon="ios-search" class="btn-search" @click="onSearchBtnClick"></Button>
    </div> -->
    <Tabs v-model="tab">
      <Button
        size="large"
        class="item"
        type="primary"
        slot="extra"
        icon="md-add"
        @click="onAddPolicy"
      >新增政策</Button>
      <TabPane label="经销商积分政策设置" name="tab1">
         <FormItem :label-width='20'> 
              <div class="common-class">
                <i class="ivu-icon ivu-icon-ios-alert"></i>
                <span> 提示：政策之间如果有规则冲突，以最后修改时间晚的为准</span>
                </div>
          </FormItem>
        <div class="table-content" v-for="(item,index) in list" :key="index">
          <div class="table-describe">
            <span>政策名称: {{item.policyName}}</span>
            <span>生效时间: {{item.beginDate}} - {{item.endDate}}</span>
            <span>最后修改时间: {{item.modifyDate}}</span>
            <Button type="primary" @click="openEdit(item)">编辑</Button>
            <Button type="primary" @click="onDelete(item.id,item.policyName)">删除</Button>
          </div>
          <Table
            stripe
            :columns="columns"
            :data="item.dataSource"
            border
            no-data-text="亲，没有找到相关记录哦！~"
          ></Table>
        </div>
        <div class="no-data" v-if="this.list.length==0">
          <p>亲,暂时没有任何政策哦！</p>
        </div>
          <Loader :status="status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import table from "@/table";
import integralData from "./list/agencyIntegralListData";
import { clearEmpty, getTireArr, formatDate } from "@/util";
export default {
  components: {},
  data() {
    return {
      keyword: "",
      status:'pass',
      tab: "tab1",
      tire_sizeArr: [],
      sizeList: [],
      storeType: [],
      brandList: [],
      list: [],
      columns: []
    };
  },
  mounted() {
    this.initData();
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    ...table,
    async initData() {
      await this.getStoreList();
      await this.getTireData();
      await this.getData();
    },
    onAddPolicy() {
      this.$router.push("/policy/agencyIntegral/edit");
    },
    openEdit(item) {
      this.$router.push({
        path: "/policy/agencyIntegral/edit",
        query: { id: item.id }
      });
    },
    // 门店类型
    getStoreList() {
      this.$ajaxGet("storeType/findAll").then(data => {
        this.storeType = data.map(item => {
          let obj = { storeTypeId: item.id, storeTypeName: item.name };
          return obj;
        });
      });
    },
    //获取尺寸
    getTireData() {
      getTireArr().then(data => {
        this.tire_sizeArr = data.data.tire_rim
          .filter(item => item.val)
          .map(item => {
            return {
              value: item.val
            };
          });
        this.tire_sizeArr.forEach(v => {
          this.sizeList.push(v["value"]);
          this.formatColumns()
        });
      });
    },
    getData() {
      this.status = "loading";
      let param = {
        name: this.keyword
      };
      param = clearEmpty(param);
      this.$ajaxGet("/setting/distributor/integral_policy/page", param)
        .then(data => {
          this.list = [];
          this.list = (data || []).map(item => {
            let dataSource = []
            item.integralPolicies.forEach(d => {
              // 存在
              let dataItem = dataSource.find(item => item.brandId === d.brandId)
              if (dataItem) {
                dataItem[d.tiresRim] = d.integral
              } else {
                let i = {
                  brandId: d.brandId,
                  brandName: d.brandName,
                }
                i[d.tiresRim] = d.integral
                dataSource.push(i)
              }
            })
            return {
              policyName: item.policyName || "--",
              beginDate: formatDate(item.beginDate),
              endDate: formatDate(item.endDate),
              modifyDate: formatDate(item.modifyDate),
              id: item.id,
              dataSource
            };
          });
          this.status='pass'
        })
        .catch((err) => {
          this.status = "error";
        });
    },
    formatColumns() {
      let columns = [];
        columns.push({
          title: '品牌',
          key: "brandName",
          align: 'left',
          fixed: "left",
          width: 100
        });
        let size = this.sizeList.map(s => {
          return {
            title: `${s}寸`,
            key: s,
            align: 'left',
            minWidth: 90
          };
        });
        this.columns = columns.concat(size);
    },
    onDelete(id, name) {
      this.$Modal.confirm({
        title: "删除",
        content: "确定删除当前政策？",
        onOk: () => {
          this.$ajaxPost(`/setting/distributor/integral_policy/delete/${id}`).then(data => {
            this.$Notice.success({
              title: "删除成功",
              desc: `<span class="font-red">${name}</span> 已被删除。`
            });
            this.getData();
          });
        }
      });
    }
  }
};
</script>